﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Security;

namespace TripLOA.GUI.secure
{
    public partial class SetFeedback : TripLOA.GUI.internationalization.LOABasePage
    {

      
        
        protected void Page_Load(object sender, EventArgs e)
        {
            TripLoaUser u = TripLoaUserManager.Instance.getUser(User.Identity.Name);
            //If user haven't feedback to release return in home page
            if (Groupware.UserManager.hasPendingFeedbacksToLeft(u.UserID) <= 0)
                        Page.Response.Redirect("Home.aspx", true);
            //Retrieve the user list of trips which he has to left one or more feedbacks.
            System.Collections.Generic.List<Stats.Objects.TripProperties> listTrips = Groupware.UserManager.getUserTripsWithFeedbacksToLeft(u.UserID);
            GridViewTrips.DataSource = listTrips;
            GridViewTrips.DataBind();
           
        }


        protected void GridViewTrips_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
        {   
            DataKey selectedTrip = GridViewTrips.DataKeys[e.NewSelectedIndex];
            int trip = Int32.Parse(selectedTrip.Values[0].ToString());
            System.Collections.Generic.List<Stats.Objects.FeedbackRequest> listFeed = Groupware.UserManager.getPendingFeedbacksForTripToLeft(Security.TripLoaUserManager.Instance.getUser(User.Identity.Name).UserID, trip);
            GridViewFeed.DataSource = listFeed;
            GridViewFeed.DataBind();
            content_feedback.Visible = true;
        }

        protected void GridViewFeed_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
        {
            
            //GridViewRow selectedRow = GridViewFeed.Rows[e.NewSelectedIndex];
            
            DataKey valuesToSend = GridViewFeed.DataKeys[e.NewSelectedIndex];
            Session["senderID_SetFeedback"] = valuesToSend.Values[0].ToString();
            Session["receiverID_SetFeedback"] = valuesToSend.Values[1].ToString();
            Session["tripID_SetFeedback"] = valuesToSend.Values[2].ToString();
            Session["feedbackType_SetFeedback"] = valuesToSend.Values[3].ToString();
            /*
             * valuesToSend.Values[0] --> SenderID
             * valuesToSend.Values[1] --> ReceiverID
             * valuesToSend.Values[2] --> TripID
             * valuesToSend.Values[3] --> FeedbackType
             * valuesToSend.Values[4] --> ReceiverUsername
             * valuesToSend.Values[5] --> TripDepartureDate
             * valuesToSend.Values[6] --> TripStartCity
             * valuesToSend.Values[7] --> TripDestinationCity
             */
            this.content_main_feedback.Visible = false;
            this.content_feedback.Visible = false;
            this.lblReceiverUsername.Text = valuesToSend.Values[4].ToString();
            this.lblTripDepartureDate.Text = valuesToSend.Values[5].ToString();
            this.lblTripStartCity.Text = valuesToSend.Values[6].ToString();
            this.lblTripDestinationCity.Text = valuesToSend.Values[7].ToString();
            this.tblFeed.Visible = true;
        }

        protected void LinkButtonResultFeedBack_Click(object sender, EventArgs e)
        {
            TripLoaUser u = TripLoaUserManager.Instance.getUser(User.Identity.Name);
            if (Groupware.UserManager.hasPendingFeedbacksToLeft(u.UserID) > 0)
                Page.Response.Redirect(Page.Request.Url.ToString(), true);
            else
                Page.Response.Redirect("Home.aspx", true);
        }

        protected void btnSendFeed_Click(object sender, EventArgs e)
        {
            bool wasPresent;
            if (RadioButtonList1.SelectedValue == "false")
                wasPresent = false;
            else wasPresent = true;
            Stats.Objects.ReleasedFeedback selectedFeed = new Stats.Objects.ReleasedFeedback(
                Int32.Parse((string)Session["senderID_SetFeedback"]),
                Int32.Parse((string)Session["receiverID_SetFeedback"]), 
                ratingGeneral.CurrentRating,
                RatingPunctuality.CurrentRating,
                RatingReliability.CurrentRating,
                RatingCourtesy.CurrentRating,
                txtComment.Text,
                Int32.Parse((string)Session["tripID_SetFeedback"]),
                Int32.Parse((string)Session["feedbackType_SetFeedback"]),
                wasPresent);
            Groupware.UserManager.releaseFeedback(selectedFeed);
            tblFeed.Visible = false;
            divResult.Visible = true;
        }
    }
}
